package com.yanqu.road.server.gameplayer.module.activity.throwingPot;

import com.yanqu.road.entity.activity.ActivityDisperseGoldInfo;
import com.yanqu.road.entity.activity.ActivityDisperseGoldResult;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ThrowingPot;
import com.yanqu.road.entity.enums.eDecorationType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.activity.ThrowingPotProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.throwingPot.ThrowingPotMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class ThrowingPotModule extends GeneralModule {
    public ThrowingPotModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {
        ActivityInfo activityInfo = ThrowingPotMgr.getActivityInfo();
        if (activityInfo != null) {
            if (ActivityMgr.activityInShowTime(activityInfo)) {
                syncActivity();
            }
        }
    }

    /**
     * 同步活动数据
     */
    public void syncActivity() {
        if (SystemOpenMgr.systemOpen(player, eSystemId.ThrowingPotActivity.getValue())) {
            //正在执行的活动消息
            ThrowingPotProto.ThrowingPotDataMsg.Builder dataMsg = ThrowingPotProto.ThrowingPotDataMsg.newBuilder();
            for (ThrowingPot throwingPot : ThrowingPotMgr.getThrowingPotMap().values()) {
                ThrowingPotProto.ThrowingPotRewardTempMsg.Builder rewardTempMsg = ThrowingPotProto.ThrowingPotRewardTempMsg.newBuilder();
                rewardTempMsg.setCombinationId(throwingPot.getCombinationId());
                rewardTempMsg.setRewards(throwingPot.getReward());
                dataMsg.addRewardTempMsgList(rewardTempMsg);
            }
            player.sendPacket(Protocol.U_THROWING_POT_DATA, dataMsg);
        }
    }

    /**
     * 飞箭送礼
     * @param activityInfo
     * @return ActivityDisperseGoldResult
     */
    public ActivityDisperseGoldResult disperseGold(ActivityInfo activityInfo, String manifesto) {
        ActivityDisperseGoldResult result = new ActivityDisperseGoldResult();
        if (activityInfo.getActivityId() != ThrowingPotMgr.getActivityInfo().getActivityId()) {
            result.setRet(GameErrorCode.E_THROWING_POT_DISPERSE_GOLD_GOOD_ARGUMENT_ERROR);
            return result;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            result.setRet(GameErrorCode.E_THROWING_POT_ACTIVITY_NO_IN_TIME);
            return result;
        }
        List<String> param3List = ThrowingPotMgr.getActivityParam3();
        List<String> consumeStr = StringUtils.stringToStringList(param3List.get(0),"=");
        Property consume = new Property(Integer.valueOf(consumeStr.get(0)),new BigInteger(consumeStr.get(1)));
        if (!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.ThrowingPot, eLogMoneyType.ThrowingPotDisperseGold)) {
            result.setRet(GameErrorCode.E_THROWING_POT_DISPERSE_GOLD_ITEM_NO_ENOUGH);
            return result;
        }

        List<Integer> decorationIdList = player.getModule(DecorationModule.class).getDecorationIdList(eDecorationType.HonoraryTitle.getValue());
        ActivityDisperseGoldInfo goldInfo = new ActivityDisperseGoldInfo();
        goldInfo.setActivityId(activityInfo.getActivityId());
        goldInfo.setType(activityInfo.getType());
        goldInfo.setUserId(player.getUserId());
        goldInfo.setManifesto(manifesto);
        goldInfo.setBeginTime(System.currentTimeMillis());
        goldInfo.setHonoraryTitles(StringUtils.listToString(decorationIdList, ","));
        if(NormalActivityMgr.hasConditionType(activityInfo.getActivityId(), eGamePlayerEventType.ThrowingPotScoreCrossRank.getValue())) {
            goldInfo.setServerList(activityInfo.getServerIdList());
        }else {
            List<Long> serverList = new ArrayList<>();
            serverList.add(GameServer.getInstance().getServerId());
            goldInfo.setServerList(serverList);
        }
        goldInfo.setUserBaseInfo(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId()));
        result.setGoldInfo(goldInfo);
        return result;
    }
}